﻿using Intex.Tools;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Linq;

namespace WPFExcelReport
{

    public class YCACOFSData : BaseFunction
    {
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public SETINTEXDB INTEXDB = new SETINTEXDB();
 
 
        public YCACOFSData()
            : base()
        {
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
        }
 
        public DataSet LoadEnv()
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataRow dwData = INTEXDB.SDDAT.NewRow();
            dwData["SDSA"] = "0178";
            SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            dwData["SDSA"] = "0196";
            SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            dwData["SDSA"] = "0312";
            SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            dwData["SDSA"] = "0649";
            SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            dwData = INTEXDB.ICC_SECTION.NewRow();
            dwData["SDSA"] = "0247";
            SResult = INTEXDBC.QuerySDDAT(dwData, 3);
            SResult.Tables[0].TableName = "ICC_SECTION";
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                row["SDDATEN"] = Helpers.Helper.GetGB2312Value(row["SDDATEN"].ToString());
            }
            SReturn.Merge(SResult);
            return SReturn;
        }
        public void UpdateProdCost(DataRow dwInput)
        {
            //INTEXDBC.InsertICC_ABPOS_MCO(null, 2, "");//插入新纪录
            INTEXDBC.UpdateICC_ABPOS_MCO(dwInput, 9999, "");//更新订单完成标志
            INTEXDBC.UpdateICC_ABPOS_MCO(dwInput, 2, "");//更新计划数量和实际数量S
            INTEXDBC.UpdateICC_ABPOS_MCO(dwInput, 3, "");//更新计划数量和实际数量M
            INTEXDBC.UpdateICC_ABPOS_MCO(dwInput, 4, "");//更新计划数量和实际数量L
            INTEXDBC.UpdateICC_ABPOS_MCO(dwInput, 5, "");//更新计划数量和实际数量XL
            INTEXDBC.UpdateICC_ABPOS_MCO(dwInput, 6, "");//更新计划数量和实际数量 总数量
            INTEXDBC.UpdateICC_ABPOS_MCO(dwInput, 7, "");//更新计划数量和实际数量：总数量差异
            INTEXDBC.UpdateICC_ABPOS_MCO(dwInput, 8, "");//加工费用
            INTEXDBC.UpdateICC_ABPOS_MCO(dwInput, 9, "");
            INTEXDBC.UpdateICC_ABPOS_MCO(dwInput, 10, "");
            INTEXDBC.UpdateICC_ABPOS_MCO(dwInput, 11, "");
            INTEXDBC.UpdateICC_ABPOS_MCO(dwInput, 12, "");
        }
        public void UpdateMatCost(DataRow dwInput)
        { 
            INTEXDBC.UpdateICC_ABAUFM(dwInput, 22);//面料的标准价格
            INTEXDBC.UpdateICC_ABAUFM(dwInput, 27);//面料颜色的标准价格
            INTEXDBC.UpdateICC_ABAUFM(dwInput, 28);//面料颜色描述
            INTEXDBC.UpdateICC_ABAUFM(dwInput, 23);//采购价格，这里没有考虑折扣附加费用
            INTEXDBC.UpdateICC_ABAUFM(dwInput, 24);//辅料标准价格
            INTEXDBC.UpdateICC_ABAUFM(dwInput, 25);//辅料颜色的标准价格
            INTEXDBC.UpdateICC_ABAUFM(dwInput, 26);//辅料尺码的标准价格
            INTEXDBC.UpdateICC_ABAUFM(dwInput, 41);//QTYACT实际消耗量从FBEW
            INTEXDBC.UpdateICC_ABAUFM(dwInput, 42);//QTYACT实际消耗量从WBEW
            INTEXDBC.UpdateICC_ABAUFM(dwInput, 51);//QTYPLAN/QTYSTD计划用量和标准用量 S
            INTEXDBC.UpdateICC_ABAUFM(dwInput, 52);//QTYPLAN/QTYSTD计划用量和标准用量 M
            INTEXDBC.UpdateICC_ABAUFM(dwInput, 53);//QTYPLAN/QTYSTD计划用量和标准用量 L
            INTEXDBC.UpdateICC_ABAUFM(dwInput, 54);//QTYPLAN/QTYSTD计划用量和标准用量 XL
        }
        public DataSet Load(DataRow dwInput, string sections)
        {
            DataSet SResult = new DataSet();
            DataSet STemp = new DataSet();
            DataSet SReturn = new DataSet();
            try
            {

                //INTEXDBC.UpdateICC_ABAUFM(null, 21);
                dwInput["ARTDES"] = Helpers.Helper.ParseSectionStr(sections);
                SResult = INTEXDBC.QueryICC_ABPOS_MCO(dwInput, 2);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }

        public DataSet LoadDetail(SETINTEXDB src, DataRow dwABCO)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();

            try
            {
                //按颜色的成本分析
                string krs = dwABCO["KRS"].ToString();
                string lfd = dwABCO["LFD"].ToString();

                DataRow dwData = INTEXDB.ABKOPF.NewRow();
                dwData["ABKMANDANT"] = "00";
                dwData["ABKAUFKRS"] = krs;
                dwData["ABKAUFLFD"] = lfd;
                SResult = INTEXDBC.QueryABKOPF(dwData);
                SReturn.Merge(SResult);
                #region=======Order======
                foreach (DataRow row in SReturn.Tables["ABKOPF"].Rows)
                {
                    dwData = INTEXDB.ABPOS.NewRow();
                    dwData["ABPMANDANT"] = row["ABKMANDANT"];
                    dwData["ABPAUFKRS"] = row["ABKAUFKRS"];
                    dwData["ABPAUFLFD"] = row["ABKAUFLFD"];
                    SResult = INTEXDBC.QueryABPOS(dwData, 99);
                    SReturn.Merge(SResult);

                    dwData = INTEXDB.ICC_ABAUFM.NewRow();
                    dwData["ABAMANDANT"] = row["ABKMANDANT"];
                    dwData["ABAVKAUFKRS"] = row["ABKAUFKRS"];
                    dwData["ABAVKAUFLFD"] = row["ABKAUFLFD"];
                    SResult = INTEXDBC.QueryICC_ABAUFM(dwData, 99);
                    SReturn.Merge(SResult);
                }
                #endregion 
                ResetDECK(SReturn);  
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        //计算成本时，考虑需求数量是米，但是采购数量是公斤
        public void ResetDECK(DataSet src)
        {
            DataSet Result = new DataSet(); 
            double colortotal = 0;
            double total = 0;
            int yc = 0;
            try
            {
                DataTable tbFabric = INTEXDB.ICC_WDECKMAT.Clone();
                tbFabric.TableName = "ICC_WDECKMAT";
                DataTable tbFMat = INTEXDB.ICC_FDECKMAT.Clone();
                tbFMat.TableName = "ICC_FDECKMAT";
                DataRow row = null;
                foreach (DataRow RX in src.Tables["ICC_ABAUFM"].Rows)
                {
                    if (RX["CONTROL"].ToString() == "Y")
                        continue;
                    if (RX["MTYPE"].ToString() == "T")
                        continue;
                    decimal RT = 1;
                    if (RX["WAUEBM"].ToString().Trim() == "KG")
                    {
                        //RX["CONTROL"] = "Y";
                        RT = Convert.ToDecimal(RX["WAFGRLFD"].ToString()) * Convert.ToDecimal(0.001);                      
                    }
                    decimal QTYSTD = System.Convert.ToDecimal(RX["QTYSTD"].ToString());
                    RX["QTYSTD"] = Math.Round(QTYSTD * RT, 1);
                    decimal QTYPLAN = System.Convert.ToDecimal(RX["QTYPLAN"].ToString());
                    RX["QTYPLAN"] = Math.Round(QTYPLAN * RT, 1);
                    decimal QTYACT = System.Convert.ToDecimal(RX["QTYACT"].ToString());
                    RX["QTYACT"] = Math.Round(QTYACT * RT, 1);
                }
                var qxd = from XD in src.Tables["ICC_ABAUFM"].AsEnumerable()
                          group XD by new
                          {
                              MTYPE = XD["MTYPE"].ToString(),
                              ART = XD["ABAART"].ToString(),
                              VAR = XD["ABAVAR"].ToString(),
                              FARBE = XD["ABAFARBE"].ToString(),
                              ASIZE = XD["ABAGROE"].ToString(),
                          }
                              into g
                              select new
                              {
                                  KEY = g.Key,
                                  UM = g.Max(XD => XD.Field<string>("WAUEBM")),
                                  FGRLFD = g.Max(XD => XD.Field<Decimal>("WAFGRLFD")),
                                  QTYSTD = g.Sum(XD => Convert.ToDecimal(XD.Field<Decimal>("QTYSTD"))),
                                  QTYPLAN = g.Sum(XD => Convert.ToDecimal(XD.Field<Decimal>("QTYPLAN"))),
                                  QTYACT = g.Sum(XD => Convert.ToDecimal(XD.Field<Decimal>("QTYACT"))),
                              };
                foreach (var XD in qxd)
                {
                    switch (XD.KEY.MTYPE.Trim())
                    {
                        case "T":
                            row = tbFMat.NewRow();
                            row["ART"] = XD.KEY.ART;
                            row["VAR"] = XD.KEY.VAR;
                            row["FARBE"] = XD.KEY.FARBE;
                            row["SIZE"] = XD.KEY.ASIZE;
                            row["UM"] = XD.UM;
                            row["QTYSTD"] = XD.QTYSTD;
                            row["QTYPLAN"] = XD.QTYPLAN;
                            row["QTYACT"] = XD.QTYACT;
                            tbFMat.Rows.Add(row);
                            break;
                        case "F":
                            row = tbFabric.NewRow();
                            row["ART"] = XD.KEY.ART;
                            row["VAR"] = XD.KEY.VAR;
                            row["FARBE"] = XD.KEY.FARBE;
                            row["UM"] = XD.UM;
                            row["FGRLFD"] = XD.FGRLFD;
                            row["QTYSTD"] = XD.QTYSTD;
                            row["QTYPLAN"] = XD.QTYPLAN;
                            row["QTYACT"] = XD.QTYACT;
                            tbFabric.Rows.Add(row);
                            break;
                    }
                }
                src.Merge(tbFMat);
                src.Merge(tbFabric);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            } 
        }
    }
}
